package com.jaks.xpense.utils;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.widget.RemoteViews;

import com.jaks.xpense.EditExpenseActivity;
import com.jaks.xpense.R;
import com.jaks.xpense.XpenseDroidActivity;
import com.jaks.xpense.XpenseDroidWidget;

public class WidgetUtils {

	public static void updateView(Context context, RemoteViews remoteViews, String today,
	    String balance) {

		remoteViews.setTextViewText(R.id.widgetToday, today);
		remoteViews.setTextViewText(R.id.widgetBalance, balance);

		if (balance.startsWith("-"))
			remoteViews.setInt(R.id.widgetBalanceLayout, "setBackgroundColor",
			    Color.parseColor("#c03109"));
		else
			remoteViews.setInt(R.id.widgetBalanceLayout, "setBackgroundColor",
			    Color.parseColor("#353535"));

		Intent addExpenseIntent = new Intent(context, EditExpenseActivity.class);
		PendingIntent pendingAddExpenseIntent = PendingIntent.getActivity(context, 0, addExpenseIntent,
		    0);
		remoteViews.setOnClickPendingIntent(R.id.widgetAddExpense, pendingAddExpenseIntent);

		Intent invokeAppIntent = new Intent(context, XpenseDroidActivity.class);
		PendingIntent pendingAppIntent = PendingIntent.getActivity(context, 0, invokeAppIntent, 0);
		remoteViews.setOnClickPendingIntent(R.id.widgetRoot, pendingAppIntent);
	}

	public static void updateWidget(Context context, RemoteViews remoteViews) {
		ComponentName component = new ComponentName(context, XpenseDroidWidget.class);
		AppWidgetManager manager = AppWidgetManager.getInstance(context);
		manager.updateAppWidget(component, remoteViews);
	}

	public static void sendUpdateToWidget(Context context, double todayTotal, double balance) {
		Intent intent = new Intent(Globals.UpdateFromActivity);
		intent.putExtra(Globals.Today, Globals.toDecimalText(todayTotal));
		intent.putExtra(Globals.Balance, Globals.toDecimalText(balance));
		context.sendBroadcast(intent);
	}
}
